# 北证A股

import json
import requests
from datetime import datetime
import os

# 假设这是当前文件的路径
script_path = __file__

# 分割文件名和扩展名
filename = os.path.splitext(os.path.basename(script_path))[0]

print(filename)
url = "https://73.push2.eastmoney.com/api/qt/clist/get"
params = {
    # "cb": "jQuery112407031859045633717_1714779534076",
    "pn": 1,
    "pz": 20,
    "po": 1,
    "np": 1,
    "ut": "bd1d9ddb04089700cf9c27f6f7426281",
    "fltt": 2,
    "invt": 2,
    "wbp2u": "|0|0|0|web",
    "fid": "f3",
    "fs": "m:0+t:81+s:2048",
    "fields": "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152",
    "_": 1714779534471
}

response = requests.get(url, params=params)

if response.status_code == 200:
    data = response.json()
    # Current date for the filename
    current_date = datetime.now().strftime("%Y-%m-%d")

    # File name with the current date
    file_name_txt = f"{current_date}_{filename}.txt"

    # Write the sample JSON data to a file
    with open(file_name_txt, 'w', encoding='utf-8') as file:
        json.dump(data, file, ensure_ascii=False, indent=4)
    print("数据已写入到 JSON 文件中。")

    # File name with the current date
    file_name_json = f"{current_date}_{filename}.json"

    # Write the sample JSON data to a file
    diff_data = data.get('data', {}).get('diff', [])
    with open(file_name_json, 'w', encoding='utf-8') as file:
        json.dump(diff_data, file, ensure_ascii=False, indent=4)
    print("数据已写入到 JSON 文件中。")

else:
    print("请求失败:", response.status_code)